package com.nsgf.library.ui.page.manager;

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.MapBean;
import com.nsgf.library.data.bean.WorkOrderDetailBean;
import com.nsgf.library.databinding.ActivityNsgfMapBinding;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.StateHolder;

import java.util.ArrayList;
import java.util.List;

public class WorkOrderMapActivity extends BaseActivity {

    StateHolder mStates;

    AccountRequester mAccountRequester;

    private ActivityNsgfMapBinding mBinding;

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(StateHolder.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_nsgf_map, BR.vm, mStates);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getLifecycle().addObserver(mAccountRequester);

        mBinding = (ActivityNsgfMapBinding) getBinding();
        String url = "file:///android_asset/roadLine/index.html";
        WebSettings mWebSettings = mBinding.webView.getWebView().getSettings();
        mWebSettings.setJavaScriptEnabled(true);//设置支持javaScript
        mBinding.webView.loadUrl(url);
        // 当页面加载完成后调用JavaScript方法
        mBinding.webView.getWebView().setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                mAccountRequester.patrolDetail(getIntent().getStringExtra("orderCode"));
            }
        });


        mAccountRequester.getWorkOrderDetail().observe(this, workOrderDetailBeanDataResult -> {

            if (200 == workOrderDetailBeanDataResult.getResult().getCode()) {
                JSONArray mapBeans = new JSONArray();
                for (WorkOrderDetailBean.DataBean.PositionVOSBean vosBean :workOrderDetailBeanDataResult.getResult().getData().getPositionVOS()){
                    JSONObject jsonObject = getJsonObject(vosBean);
                    mapBeans.add(jsonObject);
                }
                callJavaScriptDrawInitLineFunction(mapBeans);
            } else finish();

        });

    }

    public static @NonNull JSONObject getJsonObject(WorkOrderDetailBean.DataBean.PositionVOSBean vosBean) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("areaCode", vosBean.getAreaCode());
        jsonObject.put("areaName", vosBean.getAreaName());
        jsonObject.put("lat", vosBean.getLat());
        jsonObject.put("lon", vosBean.getLon());
        jsonObject.put("positionCode", vosBean.getPositionCode());
        jsonObject.put("positionName", vosBean.getPositionName());
        jsonObject.put("status",vosBean.getStatus());
        return jsonObject;
    }

    private void callJavaScriptDrawPatrolLineFunction() {
        mBinding.webView.getWebView().evaluateJavascript("drawPatrolLine();", null);
    }

    private void callJavaScriptDrawInitLineFunction(JSONArray list) {
        mBinding.webView.getWebView().evaluateJavascript("drawPatrolLine(" + "'" + list + "'" + ");", null);
    }
}
